// buffmgr.cpp : Defines the entry point for the console application.

#include <buff.h>
#include <page.h>
#include <conio.h>
#include "stdio.h"
#include <process.h>
#include "iostream"
#include <windows.h>
#include <fstream>
#include "db_header.h"

using namespace std;

BMgr obj;

DBH obj2;

FILE *fp;

void menu();

void buff_init();

void users_choice(int);

//int main()
//{
	/************************* To display the user-menu*******************************/
//	menu();
//	getch();
//}

void menu()
{

	system( "cls" );

	/****************************************MENU FOR BUFFER MANAGER*****************************/
	   
	cout<<"\n\n**************************** BUFFER MANAGER ********************************";
	cout<<"\n\n\t\t\t\t MENU\n\n";
	cout<<"__________________________________________________________________________\n";
	cout<<"\n1. Set the Page Size";
	cout<<"\n2. Initialize Buffer";
	cout<<"\n3. Show the Page";
	cout<<"\n4. Create a file";
	cout<<"\n5. Open a file";
	cout<<"\n6. Write a file";
	cout<<"\n7. Read a file";
	cout<<"\n8. Show frame details";
	cout<<"\n9. Display Frame";
	cout<<"\n10. Show Hit Ratio";
	cout<<"\n11. Close Buffer";
	cout<<"\n12. Delete File";
	cout<<"\n13. Close File";
	cout<<"\n14. Increment db";
	cout<<"\n15. Testing";
	cout<<"\n__________________________________________________________________________\n";

	/************************* User will enter his choice ****************************/
	int user_choice = -1;

	/************************* Validating choice ************************/

	while(user_choice < 1 || user_choice > 15)
	{
		cout<<"Please enter an option between 1 & 13 ..... ";
		cin>>user_choice;
	}

	/***************** Calling function according to the user's choice ******************/
	users_choice(user_choice);
}

void users_choice(int user_choice)
{
	switch(user_choice)
	{
	case 1:	/*initializing the global variable PAGE_SIZE*/
		//int PAGE_SIZE;
		int x;
		long ll;
		system( "cls" );
		cout<<"\n FRAME SIZE \n";
		cout<<"\n__________________________________________________________________________\n";
		cout<<"\n Please enter the buffer page size (in KB): ";
		//cin>>PAGE_SIZE;
		cin>>x;
		obj.setpagesize(x);
		cout<<".";
		Sleep(250);
		cout<<" .";
		Sleep(250);
		cout<<" .";
		Sleep(250);
		cout<<" .";
		cout<<" Page Size initialized to :"<<PAGE_SIZE<<" KB";
		cout<<"\n Press any key to continue";
		getch();
		menu();
		break;

	case 2:	/*initializing the Buffer Pool*/
		buff_init();
		break;

	case 3:
		break;

	case 4:
		char dbname[50];
		long numOfpages;
		long pagesize;
		system( "cls" );
		cout<<"\n CREATE FILE (TESTING MODE) \n";
		cout<<"\n__________________________________________________________________________\n";
		cout<<"\nName of new database : ";
		cin>>dbname;
		cout<<"\nEnter the number of pages :";
		cin>>numOfpages;
		cout<<"\nEnter the page size :";
		cin>>pagesize;
		ll = obj.createDB(dbname,numOfpages,pagesize);
		cout<<"\n Press any key to continue.....";
		getch();
		menu();
		break;

	case 5: /*will load systable and other structures into frames*/
		system( "cls" );
		cout<<"\n OPEN DATABASE (TESTING MODE) \n";
		cout<<"\n__________________________________________________________________________\n";
		cout<<"\nName of database : ";
		cin>>dbname;
		obj.open_DB(dbname);
		break;
	case 6:
		break;
	case 7:
		char dbn[50];
		system( "cls" );
		cout<<"\n READ PAGE \n";
		cout<<"\n__________________________________________________________________________\n";
		//cout<<"Enter the dbname to be read :";
		//cin>>dbn;
		//obj.readPage(dbn,1);
		//obj.readPage("ourdb.dbase",1);
		//readpg.readPage(dbn,1);
		break;
	case 8:
		break;
	case 9:
		long frame_number;
		long num;
		num = obj.getmemsize()/obj.getpagesize() -1;
		system( "cls" );
		cout<<"\n SHOW FRAME \n";
		cout<<"\n__________________________________________________________________________\n";
		cout<<"\n Enter the page number to be displayed (between 0 to "<<num<<" ) :";
		cin>>frame_number;
		while(frame_number < 0 || frame_number > num)
		{
			cout<<"Please enter an option between 0 & "<<num<<" : ";
			cin>>frame_number;
		}
		obj.showFrame(frame_number);
		break;
	case 10:
		break;
	case 11:

		break;
	case 12:
		break;
	case 13:
		system( "cls" );
		cout<<"\n DELETE DB FILE (TESTING MODE) \n";
		cout<<"\n__________________________________________________________________________\n";
		cout<<"\nName of new database : ";
		cin>>dbname;
		obj.close_DB(dbname);
		break;
	case 14:
		system( "cls" );
		cout<<"\n INCREMENT DB FILE (TESTING MODE) \n";
		cout<<"\n__________________________________________________________________________\n";
		cout<<"\nName of new database : ";
		cin>>dbname;
		cout<<"\nEnter the number of pages :";
		cin>>numOfpages;
		cout<<"\nEnter the page size :";
		cin>>pagesize;
		ll = obj.incrementDB(dbname,numOfpages,pagesize);
		cout<<"\n Press any key to continue.....";
		getch();
		menu();
		break;
	case 15:

		obj2.create_db("ThirdDB.dbase",10,4096);
		break;

	default:
		break;
	}
}

void buff_init()
{
		long buff_mem;
		system( "cls" );
		cout<<"\n INITIALIZING BUFFER \n";
		cout<<"\n__________________________________________________________________________\n";
		cout<<"Enter the size of the Buffer Memory(in KB) :";
		cin>>buff_mem;

		/*Object to initialize the size of Buffer*/
		//BMgr init_obj;

		/* Calling Method to do allocation */
		//init_obj.init_buffer(buff_mem,PAGE_SIZE);
		obj.init_buffer(buff_mem,obj.getpagesize());

		cout<<".";
		Sleep(250);
		cout<<" .";
		Sleep(250);
		cout<<" .";
		Sleep(250);
		cout<<" .";
		cout<<"\nAllocation done";
		cout<<"\nBuffer Space :"<<buff_mem<<" KB";
		cout<<"\nPage Size :"<<obj.getpagesize()<<" KB";
		cout<<"\nNumber of frames available :"<<buff_mem/obj.getpagesize();
		cout<<"\nPress any key to continue";
		getch();
		menu();
}
